package org.cncitrus.data.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class OrangeConn {
//	private static String DriverName = "com.mysql.jdbc.Driver";
	private static Connection con;
//	private String url = "jdbc:mysql://219.136.252.35:3310/orangedb";
//	private String user = "zxmroot";
//	private String password = "112358";
	private static String DBDRIVER,USER,PSW,URL;
	private Properties prop;
	private String propFileName = "/loclhostconnDB.properties";
	private InputStream in;
	private OrangeConn(){
			prop = new Properties();
			in = getClass().getResourceAsStream(propFileName);
			try {
				prop.load(in);
				DBDRIVER = prop.getProperty("database.driver");
				USER = prop.getProperty("database.user");
				PSW = prop.getProperty("database.password");
				URL = prop.getProperty("database.url");
				Class.forName(DBDRIVER).newInstance();	
				System.out.println(URL);
//				con = DriverManager.getConnection("jdbc:mysql://219.136.252.35:3310/orangedb?user=zxmroot&password=112358&useUnicode=true&characterEncoding=utf8");
				con = DriverManager.getConnection(URL,USER,PSW);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err.print("无法连接数据库");
			} catch (IOException e) {
				System.err.print("没有相应的配置文件");
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				System.err.print("找不到相应的包");
				e.printStackTrace();
			}
		
	}
	static private OrangeConn conn =null;
	static public OrangeConn getInstance(){
		if(conn == null){
			conn = new OrangeConn();
		}
		return conn;
	}
	public Statement getStatement() throws SQLException{
		try{
			return con.createStatement();
		}catch(SQLException e){
			conn = new OrangeConn();
			return con.createStatement();
		}
		
	}
	
}
